// pages/pub_comment/pub_comment.js
const app = getApp();
import { addEvaluate, getEvaluate } from '../../config/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', // 订单id
    list: [], // 商品列表
    stars: [0, 1, 2, 3, 4], //星星数组
    normalStar: '/assets/img/ico-star.png', //未选中的星星图片
    selectedStar: '/assets/img/ico-star-on.png', //选中的星星图片
    key: 4, //选中的星星数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    getEvaluate({
      token: app.globalData.userId,
      order_id: that.data.id,
    }).then(res => {
      wx.hideLoading();
      if(res.code == 1){
        res.data.info.forEach((item) => {
          item.star = -1;
          item.evaluate = '';
          item.imgs = [];
          item.content = ''
        })
        that.setData({
          list: res.data.info
        })
      }else{
        app.$tip(res.msg)
      }
    })
  },

  //星星评分
  clickStar(e) {
    let index = e.currentTarget.dataset.index,old = this.data.list,findex = e.currentTarget.dataset.findex;
    old[findex].star = index;
    if(index == 0){
      old[findex].evaluate = '非常差'
    }
    if(index == 1){
      old[findex].evaluate = '差'
    }
    if(index == 2){
      old[findex].evaluate = '一般'
    }
    if(index == 3){
      old[findex].evaluate = '好'
    }
    if(index == 4){
      old[findex].evaluate = '非常好'
    }
    this.setData({
      list: old
    })
  },

  // 填写评价
  initText(e){
    let findex = e.currentTarget.dataset.findex, old = this.data.list,that = this;
    old[findex].content = e.detail.value;
    this.setData({
      list: old
    })
  },

  // 添加照片
  addImg(e){
    let findex = e.currentTarget.dataset.findex, old = this.data.list,that = this;
    wx.chooseImage({
      count: 9,
      success: function(res) {
        let tempFilePaths = res.tempFilePaths;
        // 多图
        if(res.tempFilePaths.length > 1){
          let len = tempFilePaths.length;
          tempFilePaths.forEach((item,index) =>{
            wx.uploadFile({
              url: app.baseURL + 'Upload/upload',
              filePath: tempFilePaths[index],
              name: 'file',
              formData: {
                file: tempFilePaths[index],
              },
              success(data) {
                let infos = JSON.parse(data.data);
                wx.hideLoading();
                if (infos.code == 1) {
                  old[findex].imgs.push(infos.data);
                  that.setData({
                    list: old
                  })
                } else {
                  app.$tip(infos.msg);
                }

              }
            })
          })
        }else{
          // 单图
          wx.uploadFile({
            url: app.baseURL + 'Upload/upload',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              file: tempFilePaths[0],
            },
            success(data) {
              let infos = JSON.parse(data.data);
              wx.hideLoading();
              if (infos.code == 1) {
                old[findex].imgs.push(infos.data);
                that.setData({
                  list: old
                })
              } else {
                app.$tip(infos.msg);
              }

            }
          })
        }
      },
    })
  },

  // 删除图片
  delImg(e){
    let that = this,
      old = this.data.list,
      index = e.currentTarget.dataset.index,
      findex = e.currentTarget.dataset.findex,
      imgs = old[findex].imgs;
    imgs.splice(index,1);
    this.setData({
      list: old
    })
  },

  // 发表评价
  subEva(e){
    let that = this, index = e.currentTarget.dataset.index;
    // 获取评价按钮所在商品的评价信息
    let data = this.data.list[index];
    if(data.star == -1){
      app.$tip('请给商品打分!')
      return
    }
    if (!data.content){
      app.$tip('请输入评价内容!')
      return
    }
    
    app.loading();
    addEvaluate({
      token: app.globalData.userId,
      order_id: that.data.id,
      goods_id: data.goods_id,
      image: data.imgs.length > 0 ? data.imgs.toString() : '',
      content: data.content,
      star_num: Number(data.star) + 1
    }).then(res =>{
      wx.hideLoading();
      if(res.code == 1){
        app.$tip(res.msg);
        setTimeout(() =>{
          // that.getData();
          wx.navigateBack()
        },1200)
      }else{
        app.$tip(res.msg)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})